using System.Collections;
using UnityEngine;
using UnityEngine.UI;

public class TutorialRecordControl : MonoBehaviour
{
	public GameRecorder gameRecorder;

	public GameRecorderPlayer gameRecorderPlayer;

	[Space(10f)]
	public Button buttonRec;

	public Button buttonPlay;

	public Text textStatus;

	public Image buttonPlayImage;

	public Sprite spritePlay;

	public Sprite spriteStop;

	private bool showControlsButtons;

	private bool playStarted;

	private void OnDisable()
	{
		StopAll();
	}

	private void Update()
	{
		if (!showControlsButtons)
		{
			return;
		}
		if (!playStarted)
		{
			if (gameRecorderPlayer.isPlaying)
			{
				playStarted = true;
			}
		}
		else if (!gameRecorderPlayer.isPlaying)
		{
			playStarted = false;
			UpdateButtonsStates();
		}
	}

	public void RefreshRecording()
	{
		gameRecorderPlayer.RefreshRecord();
		UpdateButtonsStates();
	}

	public void OnRec()
	{
		if (gameRecorder.isRecording)
		{
			gameRecorder.StopRecording();
			StartCoroutine(RefreshLater());
		}
		else
		{
			gameRecorder.StartRecording();
		}
		UpdateButtonsStates();
	}

	public void OnPlay()
	{
		gameRecorderPlayer.loadFromResources = false;
		gameRecorderPlayer.resumeAfterPlay = true;
		if (gameRecorderPlayer.isPlaying)
		{
			gameRecorderPlayer.Stop();
		}
		else
		{
			gameRecorderPlayer.Play();
		}
		UpdateButtonsStates();
	}

	public void PlayFromResources()
	{
		gameRecorderPlayer.loadFromResources = true;
		gameRecorderPlayer.resumeAfterPlay = false;
		gameRecorderPlayer.Play(false);
	}

	private IEnumerator RefreshLater()
	{
		yield return null;
		yield return null;
		yield return null;
		gameRecorderPlayer.RefreshRecord();
		UpdateButtonsStates();
	}

	public void UpdateButtonsStates()
	{
		buttonPlay.interactable = !gameRecorder.isRecording && gameRecorderPlayer.isAvailable;
		buttonRec.interactable = !gameRecorderPlayer.isPlaying;
		buttonPlayImage.sprite = spritePlay;
		if (gameRecorder.isRecording)
		{
			textStatus.text = "REC";
			textStatus.color = Color.red;
		}
		else if (gameRecorderPlayer.isPlaying)
		{
			textStatus.text = "PLAY";
			textStatus.color = Color.green;
			buttonPlayImage.sprite = spriteStop;
		}
		else
		{
			textStatus.text = string.Empty;
		}
	}

	public void ShowControls(bool show)
	{
		showControlsButtons = show;
		buttonPlay.gameObject.SetActive(show);
		buttonRec.gameObject.SetActive(show);
		textStatus.gameObject.SetActive(show);
	}

	public void StopAll()
	{
		gameRecorder.isRecording = false;
		gameRecorderPlayer.isPlaying = false;
	}
}
